package com.ys.controller.web.business;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ys.service.business.TpProductActiveSqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.entity.business.TpProductActiveSq;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/business/tpProductActiveSq")
public class WebTpProductActiveSqController {

    @Autowired
    private CommonApiService<TpProductActiveSq> commonApiService;


    @Autowired
    private TpProductActiveSqService tpProductActiveSqService;
    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        try {
            String bid = commonApiService.getCommonNextBid(MenuId.TP_ProductActiveSq);
            BigDecimal fprice = new BigDecimal("239");
            return commonApiService.toAdd(model, new TpProductActiveSq().setBid(bid).setFprice(fprice), MenuId.TP_ProductActiveSq, null, null);
        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
//    @RequiresPermissions("tpProductActiveSq:edit")
    @GetMapping("/toEdit")
    public String toEdit(Model model, String bid, Long id) throws Exception{
        if (bid == null){
            return W.error(model, "单号不能为空");
        }

        Map<String, Object> extraParams = tpProductActiveSqService.getExtraParam(bid);
        if (extraParams == null){
            return W.error(model, "该申请已删除!");
        }

        YsProcInst yspProcInst = new YsProcInst().setBid(String.valueOf(extraParams.get("bid"))).queryFirst();
        Integer onProcess = yspProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
        model.addAttribute("onProcess", onProcess);

        return commonApiService.toEdit(model, id, new TpProductActiveSq(), MenuId.TP_ProductActiveSq, extraParams, null);
    }
    @Data
    @AllArgsConstructor
    private static class TpProductActiveSqEdit{
        private List<Map<String, Object>> tpProductActiveSq;
        private Long id;
        private String code;
    }
}
